# coding=utf-8
from scapy.all import *

count = 1000
now_time = datetime.now().strftime("%Y%m%d%H%M%S")

# filename = "./pcap/email_dns_data_{0}.pcap".format(now_time)
# # filter = 'tcp.port == 2222'
o_open_file = None


def callback(packet):
    o_open_file.write(packet)


def run():
    print("====================抓取程序====================\n")
    try:
        global o_open_file
        for i in range(10):
            filename = "/root/pcap/data_{0}_{1}.pcap".format(now_time, i)
            print("=====> 抓取文件 [", filename, ' ]中 <=====')
            o_open_file = PcapWriter(filename, append=True)
            dpkt_input = sniff(count=count, prn=callback)
        print("=====> 文件抓取完毕 <=====")
        print("\n===============================================")
    except Exception:
        print(Exception.with_traceback())
    return


if __name__ == '__main__':
    run()
